package com.nulldev.util.networking.npacket4.iprotocols.ncryptx.methods.newhope;

/**************************************************************************************************
*
* Implements objects for public and private keys including key generation functions for the New
* Hope algorithms.
*
* To improve efficiency, all elements are kept in the Fourier domain and only translated back to
* compute the shared key at the end. This avoids converting back and forth each time a ring elt
* multiplication is performed.
*  
**************************************************************************************************/

import java.util.Arrays;

public class NewHopePublicKey {
	private RingElt key;

	public NewHopePublicKey(NewHopePrivateKey k, RingElt a) {
		RingElt e = Sample.getSample();
		e.ntt();
		// Multiply by 3 because of Longa/Naehrig modular reduction optimizations
		e.multByConst(3);
		key = a.pointwiseMultAdd(k.getS(), e);
		// key.correction ();
	}

	public NewHopePublicKey(NewHopePrivateKey k, RingElt e, RingElt a) {
		e.ntt();
		key = a.pointwiseMultAdd(k.getS(), e);
	}

	public NewHopePublicKey(RingElt b) {
		key = new RingElt(b);
	}

	public NewHopePublicKey(NewHopePublicKey k) {
		key = new RingElt(k.key);
	}

	public NewHopePublicKey(byte[] inBytes) {
		key = new RingElt(inBytes);
	}

	public RingElt getKey() {
		return key;
	}

	public byte[] serialize() {
		return key.toByteArray();
	}

	public int hashcode() {
		return Arrays.hashCode(serialize());
	}
}